package com.example.SpringRestApp.dao;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.example.SpringRestApp.beans.Campaign;

@Component("CampaignRepo")
public class CampaignRepoImpl implements CampaignRepo {
	
	private Map<String, Campaign> repo;
	
	public CampaignRepoImpl() {
		repo = new HashMap<String, Campaign>();
		
		Campaign c = new Campaign();
		c.setName("Foo");
		c.setDescription("Foo description");
		repo.put(c.getName(), c);

		c = new Campaign();
		c.setName("Yeah");
		c.setDescription("Yeah description");
		repo.put(c.getName(), c);
	}

	public void addCampaign(Campaign campaign) {
		repo.put(campaign.getName(), campaign);
	}

	public Campaign getCampaign(String campaignName) {
		return repo.get(campaignName);
	}

	public List<Campaign> getCampaigns() {
		List<Campaign> l = new LinkedList<Campaign>();
		for(Campaign c : repo.values())
			l.add(c);
		return l;
	}

	public Campaign removeCampaign(String campaignName) {
		return repo.remove(campaignName);
	}
	
	public void replaceAll(List<Campaign> campaignList) {
		repo.clear();
		for(Campaign c : campaignList) {
			repo.put(c.getName(), c);
		}
	}

}
